package com.sojson.project.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.enums.EBalanceTotalType;
import com.sojson.enums.EBalanceType;
import com.sojson.project.entity.po.BalanceLog;
import com.sojson.project.entity.vo.BalanceLogVo;
import com.sojson.project.service.BalanceLogService;
import com.sojson.result.Result;
import com.sojson.util.DateUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 佣金记录表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "佣金记录表管理")
@RestController
@RequestMapping(value = "/balanceLog")
public class BalanceLogController {

    @Resource
    private BalanceLogService balanceLogServiceImpl;

    /**
     * 单条插入
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insert")
    public Object insert(@RequestBody BalanceLog balanceLog) throws SQLException {
        balanceLogServiceImpl.insert(balanceLog);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody BalanceLog balanceLog) throws SQLException {
        return Result.success(balanceLogServiceImpl.insertList(balanceLog));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody BalanceLog balanceLog) throws SQLException {
        return Result.success(balanceLogServiceImpl.deleteByDto(balanceLog));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countAll")
    public Object countAll() throws SQLException {
        int countAll = balanceLogServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findAll")
    public Object findAll() throws SQLException {
        List<BalanceLog> findAll = balanceLogServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/countByDto")
    public Object countByDto(@RequestBody BalanceLog balanceLog) throws SQLException {
        int countByDto = balanceLogServiceImpl.countByDto(balanceLog);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByDto")
    public Object findByDto(BalanceLog balanceLog) throws SQLException {
        List<BalanceLogVo> findByDto = balanceLogServiceImpl.findByDto(balanceLog);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteById")
    public Object deleteById(Integer id) throws SQLException {
        balanceLogServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        return Result.success(balanceLogServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateById")
    public Object updateById(@RequestBody BalanceLog balanceLog) throws SQLException {
        balanceLogServiceImpl.updateById(balanceLog);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody BalanceLog balanceLog) throws SQLException {
        return Result.success(balanceLogServiceImpl.updateStaticByIdList(balanceLog));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody BalanceLog balanceLog) throws SQLException {
        return Result.success(balanceLogServiceImpl.updateChangeByIdList(balanceLog));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updatePartialById")
    public Object updatePartialById(@RequestBody BalanceLog balanceLog) throws SQLException {
        balanceLogServiceImpl.updatePartialById(balanceLog);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody BalanceLog balanceLog) throws SQLException {
        return Result.success(balanceLogServiceImpl.updateStaticPartialByIdList(balanceLog));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody BalanceLog balanceLog) throws SQLException {
        return Result.success(balanceLogServiceImpl.updateChangePartialByIdList(balanceLog));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findById")
    public Object findById(Integer id) throws SQLException {
        BalanceLog findById = balanceLogServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<BalanceLog> findByIdList = balanceLogServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    // @PostMapping("/findIdByDto")
    public Object findIdByDto(@RequestBody BalanceLog balanceLog) throws SQLException {
        ArrayList<Integer> findIdByDto = balanceLogServiceImpl.findIdByDto(balanceLog);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @PostMapping("/clear")
    public Object clear() throws SQLException {
        balanceLogServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询推广列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findRegisterByDto")
    @PreAuthorize("@ss.hasPermi('/balanceLog/findRegisterByDto')")
    @ApiOperation(value = "根据对象查询推广列表", response = BalanceLogVo.class)
    public Object findRegisterByDto(BalanceLogVo balanceLog) throws SQLException {
        Date thisDate = balanceLog.getThisDate();
        if (StringUtil.isNotBlankObject(thisDate)) {
            balanceLog.setBeginTime(DateUtil.getMonthOneDays(thisDate));
            balanceLog.setOverTime(DateUtil.getNextMonthOneDays(thisDate));
        }
        List<BalanceLogVo> findByDto = balanceLogServiceImpl.findRegisterByDto(balanceLog);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询分销列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findDivideByDto")
    @PreAuthorize("@ss.hasPermi('/balanceLog/findDivideByDto')")
    @ApiOperation(value = "根据对象查询分销列表", response = BalanceLogVo.class)
    public Object findDivideByDto(BalanceLogVo balanceLog) throws SQLException {
        Date thisDate = balanceLog.getThisDate();
        if (StringUtil.isNotBlankObject(thisDate)) {
            balanceLog.setBeginTime(DateUtil.getMonthOneDays(thisDate));
            balanceLog.setOverTime(DateUtil.getNextMonthOneDays(thisDate));
        }
        List<BalanceLogVo> findByDto = balanceLogServiceImpl.findDivideByDto(balanceLog);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询推广列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findAppRegisterByDto")
    @PreAuthorize("@ss.login()")
    @ApiOperation(value = "App根据对象查询分销列表", response = BalanceLogVo.class)
    public Object findAppRegisterByDto(BalanceLogVo balanceLog) throws SQLException, IOException {
        balanceLog.setMid(TokenUtil.getUserId());
        balanceLog.setType(EBalanceType.REGISTER.getCode());
        Date thisDate = balanceLog.getThisDate();
        if (StringUtil.isNotBlankObject(thisDate)) {
            balanceLog.setBeginTime(DateUtil.getMonthOneDays(thisDate));
            balanceLog.setOverTime(DateUtil.getNextMonthOneDays(thisDate));
        }
        List<BalanceLogVo> findByDto = balanceLogServiceImpl.findRegisterByDto(balanceLog);
        BigDecimal sumBalance = balanceLogServiceImpl.sumBalance(balanceLog);

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("list", findByDto);
        hashMap.put("balance", sumBalance);
        return Result.success(hashMap, findByDto);
    }

    /**
     * 根据对象查询分销列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findAppDivideByDto")
    @PreAuthorize("@ss.login()")
    public Object findAppDivideByDto(BalanceLogVo balanceLog) throws SQLException, IOException {
        balanceLog.setMid(TokenUtil.getUserId());
        balanceLog.setType(EBalanceType.DIVIDE.getCode());
        Date thisDate = balanceLog.getThisDate();
        if (StringUtil.isNotBlankObject(thisDate)) {
            balanceLog.setBeginTime(DateUtil.getMonthOneDays(thisDate));
            balanceLog.setOverTime(DateUtil.getNextMonthOneDays(thisDate));
        }
        List<BalanceLogVo> findByDto = balanceLogServiceImpl.findDivideByDto(balanceLog);
        BigDecimal sumBalance = balanceLogServiceImpl.sumBalance(balanceLog);

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("list", findByDto);
        hashMap.put("balance", sumBalance);
        return Result.success(hashMap, findByDto);
    }

    /**
     * 根据对象查询列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findXcxByDto")
    public Object findXcxByDto(BalanceLog balanceLog) throws SQLException, IOException {
        balanceLog.setMid(TokenUtil.getUserId());
        List<BalanceLog> findByDto = balanceLogServiceImpl.findXcxByDto(balanceLog);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询列表
     * 
     * @param balanceLog
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findSjByDto")
    public Object findSjByDto(BalanceLog balanceLog) throws SQLException, IOException {
        balanceLog.setMid(TokenUtil.getUserId());
        balanceLog.setTotalType(EBalanceTotalType.BALANCE.getCode());
        balanceLog.setType(EBalanceType.SHOPPING.getCode());
        List<BalanceLogVo> findSjByDto = balanceLogServiceImpl.findSjByDto(balanceLog);
        return Result.success(findSjByDto, findSjByDto);
    }

}